﻿
using Ec.NETCoreFramework.Common;
using Ec.NETCoreFramework.Common.Core.Helpers;
using Ec.NETCoreFramework.Common.Core.Serializer;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Ec.NETCoreFramework.Common.Cache
{
    /// <summary>
    /// zset
    /// </summary>
    public class RedisZSetService: RedisDataBaseManager
    {
        public RedisZSetService(string server, string password, int dbId):base(server,password,dbId)
        {
            
        }
        /// <summary>
        /// 添加到zset中，并设置分数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="item"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        public bool AddItemToZSet<T>(string key, T item, long score)
        {
            byte[] bytes = ByteSerializer.Serialize(item);
            return _Database.SortedSetAdd(key, bytes, score);
        }
        /// <summary>
        /// 添加到zset中，并设置分数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="list"></param>
        /// <param name="score"></param>
        /// <returns></returns>
        public bool AddItemToZSet<T>(string key,List<T> list, long score)
        {
            var batch = _Database.CreateBatch();
            foreach (var item in list)
            {
                byte[] bytes = ByteSerializer.Serialize(item);
                batch.SortedSetAddAsync(key, bytes,score);
            }
            batch.Execute();
            return true;
        }
        /// <summary>
        /// 删除key为value的数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool RemoveItemsFromZSet(string key,string value)
        {
            return _Database.SortedSetRemove(key, value);
        }
        /// <summary>
        /// 删除下标从minScore到maxScore的key集合数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="minScore"></param>
        /// <param name="maxScore"></param>
        /// <returns></returns>
        public long RemoveItemsFromZSet(string key, long minScore, long maxScore)
        {
            return _Database.SortedSetRemoveRangeByScore(key, minScore, maxScore);
        }
        /// <summary>
        /// 获取key集合数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<T> GetRange<T>(string key)
        {
            var items = _Database.SortedSetRangeByScore(key);
            var list = new List<T>();
            foreach (var item in items)
            {
                byte[] bytes = (byte[])item;

                if (bytes != null && bytes.Length > 0)
                {
                    list.Add(ByteSerializer.Deserialize<T>(bytes));
                }
            }
            return list;
        }
        /// <summary>
        /// 获取key集合数据，下标从fromRank到分数为toRank的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="fromRank"></param>
        /// <param name="toRank"></param>
        /// <returns></returns>
        public List<T> GetRange<T>(string key, long fromRank, long toRank)
        {
            var items = _Database.SortedSetRangeByRank(key, fromRank, toRank);
            var list = new List<T>();
            foreach (var item in items)
            {
                byte[] bytes = (byte[])item;

                if (bytes != null && bytes.Length > 0)
                {
                    list.Add(ByteSerializer.Deserialize<T>(bytes));
                }
            }
            return list;
        }
    }
}
